package io.kiki.stack.http.feign;

import io.kiki.stack.http.feign.Request.HttpMethod;

import java.io.Serial;
import java.util.Collection;
import java.util.Date;
import java.util.Map;


public class RetryableException extends FeignException {

    @Serial
    private static final long serialVersionUID = 2L;

    private final Long retryAfter;
    private final HttpMethod httpMethod;


    public RetryableException(int status, String message, HttpMethod httpMethod, Throwable cause, Long retryAfter, Request request) {
        super(status, message, request, cause);
        this.httpMethod = httpMethod;
        this.retryAfter = retryAfter;
    }

    @Deprecated
    public RetryableException(int status, String message, HttpMethod httpMethod, Throwable cause, Date retryAfter, Request request) {
        super(status, message, request, cause);
        this.httpMethod = httpMethod;
        this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
    }


    @Deprecated
    public RetryableException(int status, String message, HttpMethod httpMethod, Date retryAfter, Request request) {
        super(status, message, request);
        this.httpMethod = httpMethod;
        this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
    }


    @Deprecated
    public RetryableException(int status, String message, HttpMethod httpMethod, Date retryAfter, Request request, byte[] responseBody, Map<String, Collection<String>> responseHeaders) {
        super(status, message, request, responseBody, responseHeaders);
        this.httpMethod = httpMethod;
        this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
    }

    /**
     * Sometimes corresponds to the {@link Util#RETRY_AFTER} header present in {@code 503}
     * status. Other times parsed from an application-specific response. Null if unknown.
     */
    public Long retryAfter() {
        return retryAfter;
    }

    public HttpMethod method() {
        return this.httpMethod;
    }
}
